package com.bluejazz.controller;

import com.bluejazz.annotation.ResponseResult;
import com.bluejazz.application.dto.OrderDTO;
import com.bluejazz.application.service.OrderService;
import com.bluejazz.controller.vo.OrderVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import javax.validation.constraints.Min;

@RestController
@RequestMapping("orders")
@ResponseResult
@Validated
public class OrderController {
    private OrderService orderService;

    @Autowired
    public void setOrderService(OrderService orderService) {
        this.orderService = orderService;
    }

    @GetMapping("/normal/{orderId}")
    public OrderVO getOrder(@Valid @Min(value = 1000, message = "订单号必须大于1000") @PathVariable("orderId") Integer orderId) {
        return orderService.getOrder(orderId);
    }

    @GetMapping("/abnormal")
    public OrderVO getOrderFail() {
        return orderService.getOrderFail();
    }

    @GetMapping("/general-error")
    public String generalError() {
        return orderService.foo();
    }

    @PostMapping("/order:save")
    public OrderVO saveOrder(@Valid @RequestBody OrderDTO orderDTO) {
        return orderService.getOrder(1);
    }
}
